import { _decorator, Component, director, find, instantiate, Label, Node, NodeEventType } from 'cc';
import { toFixedString } from '../Utils';
import { NetworkManager } from '../Runtime/NetworkManager';
import { ApiMsgEnum, IRoom } from '../Common';
import DataManager from '../Runtime/DataManager';
import { SceneEnum } from '../Enum';
import { showToast } from '../Runtime/MainManager';
const { ccclass, property } = _decorator;

@ccclass('HallScene')
export class HallScene extends Component {

    @property(Node)
    item: Node

    @property(Node)
    content: Node

    @property([Label])
    lab_names: Label[] = []

    @property(Label)
    lab_roomName: Label

    private data: IRoom[] = []

    private selectedRoomId: number = 0

    onLoad() {

        for (const i in this.lab_names) {
            this.lab_names[i].string = ''
        }
    }
    start() {

        NetworkManager.Ins.callApi(ApiMsgEnum.ApiRoomList, {}).then(({ success, res, error }) => {
            if (!success) {
                showToast(error)
                console.log(error)
                return
            }
            this.data = res.rooms
            this.render()
            NetworkManager.Ins.listening(ApiMsgEnum.MsgHallSync, this.handelMsgHallSync, this)
        })
    }

    onCreate() {
        NetworkManager.Ins.callApi(ApiMsgEnum.ApiRoomCreate, {}).then(({ success, res, error }) => {
            if (!success) {
                showToast(error)
                console.log(error)
                return
            }
            DataManager.Ins.roomInfo = res.room
            director.loadScene(SceneEnum.Room)
        })
    }

    onJoin() {
        const rid = this.data[this.selectedRoomId]?.rid
        NetworkManager.Ins.callApi(ApiMsgEnum.ApiRoomJoin, { rid }).then(({ success, res, error }) => {
            if (!success) {
                showToast(error)
                console.log(error)
                return
            }
            DataManager.Ins.roomInfo = res.room
            director.loadScene(SceneEnum.Room)
        })
    }

    /** 大厅内玩家同步信息 */
    handelMsgHallSync({ rooms }) {
        this.data = rooms
        this.render()
    }

    render() {

        this.content.removeAllChildren()
        let selectedRoom: IRoom
        for (const i in this.data) {
            const room = this.data[i]
            const node = instantiate(this.item)
            const str = toFixedString(room.players[0].nickName) + '的房间'
            node.getChildByName('lab_name').getComponent(Label).string = str
            node.getChildByName('lab_number').getComponent(Label).string = room.players.length + '/4'

            node.parent = this.content
            node.setPosition(0, 0)
            node.off(NodeEventType.TOUCH_END)
            find('bg/selected_bg', node).active = this.selectedRoomId == Number(i)
            node.on(NodeEventType.TOUCH_END, () => {
                this.selectedRoomId = Number(i)
                this.render()
            })
        }

        selectedRoom = this.data[this.selectedRoomId]
        if (selectedRoom) {
            this.lab_roomName.string = selectedRoom.isGaming ? '游戏中' : selectedRoom.players[0].nickName + '的房间'
        } else {
            this.lab_roomName.string = ''
        }

        for (const i in this.lab_names) {
            const lab = this.lab_names[i]
            lab.string = ''
            if (selectedRoom) {
                const nickName = selectedRoom.players[i]?.nickName
                lab.string = nickName ? toFixedString(selectedRoom.players[i].nickName) : '空位'
            }
        }
    }

    protected onDestroy(): void {
        NetworkManager.Ins.unlistening(ApiMsgEnum.MsgHallSync, this.handelMsgHallSync, this)

    }

}


